package com.dht.xiao.jianye.activity_bed;

import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.dht.xiao.jianye.R;
import com.dht.xiao.jianye.adapter.BedGetAdapter;
import com.dht.xiao.jianye.adapter.BedQueryAdapter;
import com.dht.xiao.jianye.base.BaseActivity;
import com.dht.xiao.jianye.bean.newdata.OfficeBean;
import com.dht.xiao.jianye.bean.newdata.ProductListBean;
import com.dht.xiao.jianye.bean.newdata.RfidQueryBean;
import com.dht.xiao.jianye.dialog.SureNewDialog;
import com.dht.xiao.jianye.http.MainHttp;
import com.dht.xiao.jianye.utils.Constants;
import com.licheedev.myutils.LogPlus;
import com.trello.rxlifecycle2.android.ActivityEvent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.dlc.commonlibrary.okgo.rx.OkObserver;
import cn.dlc.commonlibrary.ui.widget.CommonItemView;
import cn.dlc.commonlibrary.ui.widget.TitleBar;
import cn.dlc.commonlibrary.ui.widget.popup.CommonPopupList;
import cn.dlc.commonlibrary.utils.ToastUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class BedQueryActivity extends BaseActivity {
    @BindView(R.id.mTitleBar)
    TitleBar mTitleBar;

    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    BedQueryAdapter mRecyclerAdapter;
    @BindView(R.id.tv_offic)
    TextView tvOffic;
    @BindView(R.id.item_offic)
    CommonItemView itemOffic;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.item_name)
    CommonItemView itemName;
    @BindView(R.id.drop_offic)
    FrameLayout dropOffic;
    @BindView(R.id.drop_name)
    FrameLayout dropName;
    private CommonPopupList mNamePopup;
    private List<ProductListBean.DataBean> mNameList;

    private CommonPopupList mOfficPopup;
    private List<OfficeBean.DataBean> mOfficList;
    String offic_id = "", name_id = "";
    String ANDROID_ID;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bed_query;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bed_query);
        ButterKnife.bind(this);
        mTitleBar.leftExit(this);
        initRecyclerView();
        initview();
        getMachineCode();
    }
    private void getMachineCode() {
        ANDROID_ID =
                Settings.System.getString(getContentResolver(), Settings.System.ANDROID_ID);
        if (TextUtils.isEmpty(ANDROID_ID)) {
            ANDROID_ID = Build.SERIAL;
        }
       // ANDROID_ID = "6f4b9da9f38b5566";
        LogPlus.e("ANDROID_ID =" + ANDROID_ID);

    }
    private void initRecyclerView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        mRecyclerAdapter = new BedQueryAdapter();
        mRecyclerView.setAdapter(mRecyclerAdapter);
    }
    @OnClick({R.id.drop_offic, R.id.drop_name, R.id.btn_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.drop_offic:
                mOfficPopup.showAsDropDown(view);
                break;
            case R.id.drop_name:
                mNamePopup.showAsDropDown(view);
                break;
            case R.id.btn_search:
                rfidQuery();
                break;
        }
    }

    private void initview() {

        mOfficPopup = CommonPopupList.newBuilder(this)
                .setWidthEquals(dropOffic)
                .setOnSelectItemListener(new CommonPopupList.OnSelectItemListener() {
                    @Override
                    public void onSelectItem(CommonPopupList popup, String item, int position) {
                        popup.dismiss();
                        tvOffic.setText(item);
                        if (mOfficList != null) {
                            offic_id = mOfficList.get(position).id;

                        }

                    }
                })
                .build();
        mNamePopup = CommonPopupList.newBuilder(this)
                .setWidthEquals(dropName)
                .setOnSelectItemListener(new CommonPopupList.OnSelectItemListener() {
                    @Override
                    public void onSelectItem(CommonPopupList popup, String item, int position) {
                        popup.dismiss();
                        tvName.setText(item);
                        if (mNameList != null) {
                            name_id =String.valueOf( mNameList.get(position).id);
                        }

                    }
                })
                .build();
        getOffice();
        initProductPopupList();
    }



    private void getOffice() {

        MainHttp.get().getOffice()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new OkObserver<OfficeBean>() {
                    @Override
                    public void onSuccess(OfficeBean officeBean) {
                        List<String> datas = new ArrayList<>();
                        mOfficList = officeBean.data;
                        for (OfficeBean.DataBean bean : officeBean.data) {
                            datas.add(bean.title);
                        }
                        mOfficPopup.setData(datas);
                    }

                    @Override
                    public void onFailure(String message, Throwable tr) {
                        ToastUtil.show(getActivity(), message);
                    }
                });
    }

    /**
     * 初始化产品下拉
     */
    private void initProductPopupList() {
        MainHttp.get().goods()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new OkObserver<ProductListBean>() {
                    @Override
                    public void onSuccess(ProductListBean productListBean) {
                      //  showOneToast(productListBean.msg);
                        mNameList = productListBean.data;

                        List<String> datas = new ArrayList<>();
                        for (ProductListBean.DataBean bean : productListBean.data) {
                            datas.add(bean.title);
                        }
                        mNamePopup.setData(datas);
                    }

                    @Override
                    public void onFailure(String message, Throwable tr) {
                        ToastUtil.show(getActivity(), message);
                    }
                });
    }

    private void rfidQuery() {
//        if (TextUtils.isEmpty(offic_id)) {
//            showToast("请先选择科室!");
//            return;
//        }
        MainHttp.get().rfidQuery(ANDROID_ID,offic_id,name_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new OkObserver<RfidQueryBean>() {
                    @Override
                    public void onSuccess(RfidQueryBean productListBean) {

                        if (productListBean.data != null&& productListBean.data.size()==0){
                            showOneToast("数据为空");
                        }
                        mRecyclerAdapter.setNewData(productListBean.data);
                    }

                    @Override
                    public void onFailure(String message, Throwable tr) {
                        ToastUtil.show(getActivity(), message);
                    }
                });
    }



}


